package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.collect.UserIntroduceActivity;
import net.gyapp.markim.module.contact.adapter.SearchResultAdapter;
import net.gyapp.markim.module.contact.vo.GroupListVo;
import net.gyapp.markim.module.contact.vo.UserVo;
import net.gyapp.markim.module.message.ConversationActivity;
import net.gyapp.markim.utils.RequestParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/12.
 * Desc:
 */
public class SearchResultActivity extends WrapperSwipeActivity<CommonPresenter> {
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    public final static int TYPE_USER = 10001;
    public final static int TYPE_GROUP = 10002;

    public int type;
    SearchResultAdapter adapter;

    public static Intent getUserIntent(Context context, List<UserVo.UserInfoBean> list) {
        return new Intent(context, SearchResultActivity.class).putExtra("type", TYPE_USER).putExtra("list", new ArrayList<>(list));
    }

    public static Intent getGroupIntent(Context context, List<GroupListVo.FindGroupsBean> list) {
        return new Intent(context, SearchResultActivity.class).putExtra("type", TYPE_GROUP).putExtra("list", new ArrayList<>(list));
    }

    @Override
    protected int getViewLayout() {
        return R.layout.common_recycler;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("搜索结果");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new SearchResultAdapter();
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                Object item = adapter.getItem(position);
                if (item instanceof UserVo.UserInfoBean) {
                    UserVo userVo = new UserVo();
                    userVo.userInfo = (UserVo.UserInfoBean) item;
                    startActivity(UserIntroduceActivity.getIntent(mActivity, userVo));
                } else if (item instanceof GroupListVo.FindGroupsBean) {
                    GroupListVo.FindGroupsBean bean = (GroupListVo.FindGroupsBean) item;
                    if (bean.groupStatus == 1 || bean.groupStatus == 4) {
                        applyJoinGroup(((GroupListVo.FindGroupsBean) item).groupNumber);
                    } else {
                        ConversationEntity entity = ImHelper.generateConversation(bean.groupNumber);
                        if (entity != null) {
                            startActivity(ConversationActivity.getIntent(mActivity, entity));
                        } else {
                            applyJoinGroup(((GroupListVo.FindGroupsBean) item).groupNumber);
                        }
                    }
                }
            }
        });


        ArrayList<Object> list = new ArrayList<>();
        type = intent.getIntExtra("type", TYPE_USER);
        switch (type) {
            case TYPE_USER:
                ArrayList<UserVo.UserInfoBean> userList = (ArrayList<UserVo.UserInfoBean>) intent.getSerializableExtra("list");
                list.addAll(userList);
                break;
            case TYPE_GROUP:
                ArrayList<GroupListVo.FindGroupsBean> groupList = (ArrayList<GroupListVo.FindGroupsBean>) intent.getSerializableExtra("list");
                list.addAll(groupList);
                break;
        }
        adapter.setAdapterData(list);
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_JOIN_GROUP)) {
            showToast("申请已提交");
        }
    }

    private void applyJoinGroup(String groupNumber) {
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_JOIN_GROUP, new RequestParams().put("groupNumber", groupNumber).get(), BaseVo.class);
    }

}
